<?php
namespace appin\cms;

use appin\BaseController;

class WebController extends BaseController
{
    /**
     * @var string 标记当前访问的客户端 web 电脑 wap 手机浏览器 wx 微信浏览器
     */
    protected $channel = 'pc';
    protected $theme = 'web';

    /**
     * 初始化参数
     */
    public function initialize()
    {
        parent::initialize();
        $this->initChannel();
    }

    /**
     * 初始化 标记当前访问的客户端
     */
    public function initChannel()
    {

        if ( $this->request->isMobile() ) {
            $this->channel = 'wap';
            $this->theme = 'wap';
        }

        if (is_weixin() === true) {
            $this->channel = 'wx';
            $this->theme = 'wap';
        }
    }

    /**
     * @param string $template
     * @param array $data
     * @return \think\response\View
     */
    public function render($template = '', $data = [])
    {
        $appin = $this->app->appin;
        $system = $this->app->system;

        if ((int) $system['has_wap'] === 2) {
            $this->theme = 'web';
        }

        if ( empty($template) ) {
            $template = $appin['name'].'/template/'.$this->theme.'/'
                .str_replace('.','/', strtolower($appin['controller'])).'/'
                .$appin['action'];
        } else {
            $template = $appin['name'] . '/template/'.$this->theme.'/' . $template;
        }
        return parent::render($template, $data);
    }
}